﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="HouseHoldingRunEngine.cs" company="DST Nexdox">
//   Copyright (c) DST Nexdox. All rights reserved.
// </copyright>
// <summary>
//   Defines the HouseHoldingRunEngine type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace BusinessEngines
{
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using BusinessEngineInterfaces;
  using Entities;
  using FileRepository.Interfaces;
  using ServiceInterfaces;

  public class HouseHoldingRunEngine : IHouseHoldingRunEngine
  {
    private readonly IHouseHoldingFileRepository houseHoldingFileRepository;
    private readonly IHouseHoldingRunService houseHoldingRunService;
    private readonly IDocumentService documentService;
    private readonly IGridRunService gridRunService;

    public HouseHoldingRunEngine(
      IHouseHoldingFileRepository houseHoldingFileRepository,
      IHouseHoldingRunService houseHoldingRunService,
      IDocumentService documentService,
      IGridRunService gridRunService)
    {
      this.houseHoldingFileRepository = houseHoldingFileRepository;
      this.houseHoldingRunService = houseHoldingRunService;
      this.documentService = documentService;
      this.gridRunService = gridRunService;
    }

    public void ProcessHouseHoldingRun()
    {
      HouseHoldingRunData houseHoldingRunData = this.houseHoldingFileRepository.GetHouseHoldingData();

      HouseHoldingRun houseHoldingRun = this.houseHoldingRunService.CreateHouseHoldingRun(
        houseHoldingRunData.Grid,
        houseHoldingRunData.StartDate,
        houseHoldingRunData.EndDate);

      foreach (String grid in houseHoldingRunData.ProcessingGridRun)
      {
        this.gridRunService.UpdateAsHouseheld(grid, houseHoldingRun.Id);
      }

      IEnumerable<String> houseHeldDocumentGuids = houseHoldingRunData.DocumentRunData.Select(drd => drd.DocumentId);

      IEnumerable<Document> houseHeldDocuments = this.documentService.GetDocuments(houseHeldDocumentGuids);

      IEnumerable<Int32> houseHeldDocumentIds = houseHeldDocuments.Select(d => d.Id);

      this.documentService.UpdateAsHouseheld(houseHeldDocumentIds, houseHoldingRun.Id);
    }
  }
}
